package fr.afcepf.iziticket.dao;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.support.DataAccessUtils;
import org.springframework.orm.hibernate3.HibernateTemplate;

import fr.afcepf.iziticket.dao.interfaces.ObjectDao;

public abstract class ObjectDaoImpl<T> implements ObjectDao<T> {

    protected HibernateTemplate hibernateTemplate;
    protected SessionFactory sessionFactory;
    private String tableName;
    
    public void setSessionFactory(SessionFactory sessionFactory)
    {
        hibernateTemplate = new HibernateTemplate(sessionFactory);
        this.sessionFactory = sessionFactory;
    }
    
    public String getTableName()
    {
          return this.tableName;
    }
    
    public void setTableName(String name)
    {
        this.tableName = name;
    }
    
    
    public void save(T paramObject) {
        hibernateTemplate.saveOrUpdate(paramObject);
    }

    @SuppressWarnings("unchecked")
    public List<T> getAll() {
        return (List<T>) hibernateTemplate.find("from " + this.getTableName());

    } 
    
    public abstract T get(int paramId);


    public void delete(T paramObject) {
        hibernateTemplate.delete(paramObject);
    }


	public int getCount() throws DataAccessException{
		String query = "select count(*) from " + this.getTableName();
		return DataAccessUtils.intResult(hibernateTemplate.find(query)) ;
	}
    

}
